using System;
using System.Data;
using System.Data.SqlClient;
using NNS.Framework;
using QLTB.BusinessObjects;

namespace QLTB.DataAccess
{
    /// <summary>
    /// Project: NNS System 
    /// Generated Class for Table : TBL_nhanvien.
    /// Date: 01/06/2010
    /// Author: Trần Hoàng Tâm
    /// </summary>
    public class TBL_nhanvienDA : Object
    {
        #region Exist

        public bool Exist(string tenuser)
        {
            bool result = false;
            SqlDataReader dr = SqlHelper.Helper.ExecuteReader(CommandType.Text,
                                                              "Select * from TBL_nhanvien Where tenuser =@tenuser",
                                                              new[] {new SqlParameter("@tenuser", tenuser)});
            if (dr == null)
            {
                SqlHelper.Helper.Close();
                return false;
            }

            if (dr.Read())
            {
                result = dr.HasRows;
            }
            dr.Close();
            SqlHelper.Helper.Close();
            return result;
        }

        #endregion

        #region Get

        public TBL_nhanvien Get(
            string tenuser)
        {
            var obj = new TBL_nhanvien();
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader(CommandType.Text,
                                                                    "Select * from TBL_nhanvien Where tenuser =@tenuser",
                                                                    new[] {new SqlParameter("@tenuser", tenuser)});
            if (myReader != null)
            {
                if (myReader.Read())
                {
                    obj = Populate(myReader);
                }
            }

            if (myReader != null)
            {
                myReader.Close();
                myReader.Dispose();
            }
            SqlHelper.Helper.Close();
            return obj;
        }

        public TBL_nhanvien Get(
            int id)
        {
            var obj = new TBL_nhanvien();
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader(CommandType.Text,
                                                                    "Select * from TBL_nhanvien Where id =@id",
                                                                    new[] {new SqlParameter("@id", id)});

            if (myReader != null)
            {
                if (myReader.Read())
                {
                    obj = Populate(myReader);
                }
                myReader.Close();
                myReader.Dispose();
            }
            SqlHelper.Helper.Close();
            return obj;
        }

        #endregion

        #region Populate

        public TBL_nhanvien Populate(
            IDataReader myReader)
        {
            TBL_nhanvien _obj = null;

            if (myReader != null)
            {
                _obj = new TBL_nhanvien();
                if (!myReader.IsDBNull(myReader.GetOrdinal("id")))
                {
                    _obj.id = (int) myReader["id"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("iddonvi")))
                {
                    _obj.iddonvi = (int) myReader["iddonvi"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("tenuser")))
                {
                    _obj.tenuser = (string) myReader["tenuser"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("matkhau")))
                {
                    _obj.matkhau = (string) myReader["matkhau"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("hoten")))
                {
                    _obj.hoten = (string) myReader["hoten"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("idchucdanh")))
                {
                    _obj.idchucdanh = (int) myReader["idchucdanh"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("idnhom")))
                {
                    _obj.idnhom = (int) myReader["idnhom"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("toantinh")))
                {
                    _obj.toantinh = (bool) myReader["toantinh"];
                }
            }
            return _obj;
        }

        #endregion

        #region Add

        public int Insert(
            int id,
            int iddonvi,
            string tenuser,
            string matkhau,
            string hoten,
            int idchucdanh,
            int idnhom,
            bool toantinh)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_nhanvien_Insert",
                                                    new[]
                                                        {
                                                            new SqlParameter("@iddonvi", iddonvi),
                                                            new SqlParameter("@tenuser", tenuser),
                                                            new SqlParameter("@matkhau", matkhau),
                                                            new SqlParameter("@hoten", hoten),
                                                            new SqlParameter("@idchucdanh", idchucdanh),
                                                            new SqlParameter("@idnhom", idnhom),
                                                            new SqlParameter("@toantinh", toantinh)
                                                        });
        }

        #endregion

        #region Update

        public int Update(
            int id,
            int iddonvi,
            string tenuser,
            string matkhau,
            string hoten,
            int idchucdanh,
            int idnhom,
            bool toantinh)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_nhanvien_Update",
                                                    new[]
                                                        {
                                                            new SqlParameter("@id", id),
                                                            new SqlParameter("@iddonvi", iddonvi),
                                                            new SqlParameter("@tenuser", tenuser),
                                                            new SqlParameter("@matkhau", matkhau),
                                                            new SqlParameter("@hoten", hoten),
                                                            new SqlParameter("@idchucdanh", idchucdanh),
                                                            new SqlParameter("@idnhom", idnhom),
                                                            new SqlParameter("@toantinh", toantinh)
                                                        });
        }

        public int ChangePassword(string tenuser, string matkhau)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.Text,
                                                    "UPDATE TBL_nhanvien SET matkhau =@matkhau WHERE tenuser=@tenuser",
                                                    new[]
                                                        {
                                                            new SqlParameter("@tenuser", tenuser),
                                                            new SqlParameter("@matkhau", matkhau)
                                                        });
        }

        #endregion

        #region Delete

        public int Delete(
            int id)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_nhanvien_Delete",
                                                    new[] {new SqlParameter("@id", id)});
        }

        #endregion
    }
}